package metric

import (
	"gddgame.cc/galaxy/binocle"
	"github.com/toolkits/nux"
)

var USES = map[string]struct{}{
	"PruneCalled":        {},
	"LockDroppedIcmps":   {},
	"ArpFilter":          {},
	"TW":                 {},
	"DelayedACKLocked":   {},
	"ListenOverflows":    {},
	"ListenDrops":        {},
	"TCPPrequeueDropped": {},
	"TCPTSReorder":       {},
	"TCPDSACKUndo":       {},
	"TCPLoss":            {},
	"TCPLostRetransmit":  {},
	"TCPLossFailures":    {},
	"TCPFastRetrans":     {},
	"TCPTimeouts":        {},
	"TCPSchedulerFailed": {},
	"TCPAbortOnMemory":   {},
	"TCPAbortOnTimeout":  {},
	"TCPAbortFailed":     {},
	"TCPMemoryPressures": {},
	"TCPSpuriousRTOs":    {},
	"TCPBacklogDrop":     {},
	"TCPMinTTLDrop":      {},
}

func Netstat(agent binocle.Service) error {
	if _, err := nux.Netstat("TcpExt"); err != nil {
		return err
	}

	agent.AddMetrics().Collect(func() map[string]float32 {
		tcpExts, _ := nux.Netstat("TcpExt")
		collects := map[string]float32{}
		for key, val := range tcpExts {
			if _, ok := USES[key]; !ok {
				continue
			}
			collects["TcpExt."+key] = float32(val)
		}
		return collects
	})

	return nil
}
